package com.hongdee.scm.estimatedprice.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.estimatedprice.entity.TScmEstimatedPrice;
import com.hongdee.scm.estimatedprice.service.ITScmEstimatedPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 名  称:TScmEstimatedPriceController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.estimatedprice.controller
 * 作  者: 术洪超
 * 日  期:2017-06-05 19:28
 */
@RestController
@RequestMapping("/scm/estimatedprice")
public class TScmEstimatedPriceController {


    @Autowired
    private ITScmEstimatedPriceService itScmEstimatedPriceService;

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择暂估价格！");
        }
        Map map = itScmEstimatedPriceService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param data 要保存的实体
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmEstimatedPrice data) throws Exception {
        TScmEstimatedPrice tm = this.itScmEstimatedPriceService.save(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(tm);
        return res;
    }


    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param start 起始记录
     * @param limit 页面大小
     * @param configureCode  配置代码
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "configureCode",defaultValue = "") String configureCode,
                    @RequestParam(name = "carModelId",defaultValue = "") String carModelId) throws Exception {

        Map map = itScmEstimatedPriceService.page(start, limit, configureCode, uid,carModelId);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }


}
